package database;

import info.*;
import messages.admin.*;
import messages.flight.Flight_Query_Ack_Message1;
import messages.flight.Flight_Query_Ack_Message2;
import messages.flight.Flight_Query_Res_Message1;
import messages.flight.Flight_Query_Res_Message2;
import messages.order.*;
import messages.passenger.Passenger_Infor_Ack_Message;
import messages.passenger.Passenger_Infor_Res_Message;
import messages.ticket.Query_Ticket_Ack_Message;
import messages.ticket.Query_Ticket_Res_Message;
import messages.ticket.Ticket_Query_Ack_Message;
import messages.ticket.Ticket_Query_Res_Message;
import messages.user.*;
import server.MessageServer;

import java.io.IOException;
import java.util.List;

public class TableDatabaseUsage {
    private static TableDatabaseUsage instance = new TableDatabaseUsage();

    public static TableDatabaseUsage getInstance(){
        return instance;
    }

    public List<Flight> getFlightList(){
        Flight_Query_Res_Message flight_query_res_message = new Flight_Query_Res_Message();
        Flight_Query_Ack_Message flight_query_ack_message;
        try {
            MessageServer.getInstance().getOos().writeObject(flight_query_res_message);
            System.out.println("客户端发送请求成功！"+flight_query_res_message);
            flight_query_ack_message = (Flight_Query_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+flight_query_ack_message);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return flight_query_ack_message.getFlightList();
    }

    public void insertToOrder(Order order){
        Order_Insert_Res_Message order_insert_res_message = new Order_Insert_Res_Message();
        Order_Insert_Ack_Message order_insert_ack_message;

        order_insert_res_message.setOrder(order);

        try {
            MessageServer.getInstance().getOos().writeObject(order_insert_res_message);
            System.out.println("客户端发送请求成功！"+order_insert_res_message);
            order_insert_ack_message = (Order_Insert_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+order_insert_ack_message);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Order> getOrderList(){
        Order_Query_Res_Message order_query_res_message = new Order_Query_Res_Message();
        Order_Query_Ack_Message order_query_ack_message;
//        order_query_res_message.setIDNO(String.valueOf(getPassengerInforList().get(0)));


        try {
            MessageServer.getInstance().getOos().writeObject(order_query_res_message);
            System.out.println("客户端发送请求成功！"+order_query_res_message);
            order_query_ack_message = (Order_Query_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+order_query_ack_message);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return order_query_ack_message.getOrderList();
    }
    public List<Order> getOrderList1(String idno){
        Order_Query_Res_Message order_query_res_message = new Order_Query_Res_Message();
        Order_Query_Ack_Message order_query_ack_message;
        order_query_res_message.setIDNO(idno);
        System.out.println(idno);


        try {
            MessageServer.getInstance().getOos().writeObject(order_query_res_message);
            System.out.println("客户端发送请求成功！"+order_query_res_message);
            order_query_ack_message = (Order_Query_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+order_query_ack_message);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return order_query_ack_message.getOrderList();
    }


    public List<FlightTicket> getFlightTicketList(String EDPA,String DEST){
        Flight_Part_Query_Res_Message flight_part_query_res_message = new Flight_Part_Query_Res_Message(EDPA,DEST);
        Flight_Part_Query_Ack_Message flight_part_query_ack_message;

        try {
            MessageServer.getInstance().getOos().writeObject(flight_part_query_res_message);
            System.out.println("客户端发送请求成功！"+flight_part_query_res_message);
            flight_part_query_ack_message = (Flight_Part_Query_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+flight_part_query_ack_message);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return flight_part_query_ack_message.getFlightTicketList();
    }

    public List<Flight> getFlightListByAddress(String address){
        Flight_Address_Query_Res_Message flight_address_query_res_message = new Flight_Address_Query_Res_Message(address);
        Flight_Address_Query_Ack_Message flight_address_query_ack_message;

        try {
            MessageServer.getInstance().getOos().writeObject(flight_address_query_res_message);
            System.out.println("客户端发送请求成功！"+flight_address_query_res_message);
            flight_address_query_ack_message = (Flight_Address_Query_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+flight_address_query_ack_message);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return flight_address_query_ack_message.getFlightList();
    }













    public List<Flight> getFlightList1(String FlightID){
        Flight_Query_Res_Message1 flight_query_res_message1 = new Flight_Query_Res_Message1();
        Flight_Query_Ack_Message1 flight_query_ack_message1;
        flight_query_res_message1.setFlightId(FlightID);
        try {
            MessageServer.getInstance().getOos().writeObject(flight_query_res_message1);
            System.out.println("客户端发送请求成功！"+flight_query_res_message1);
            flight_query_ack_message1 = (Flight_Query_Ack_Message1) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+flight_query_ack_message1);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return flight_query_ack_message1.getFlightList();
    }


    /*按地点查询*/
    public List<Flight> getFlightList2(String FlightArea){
        Flight_Query_Res_Message2 flight_query_res_message2 = new Flight_Query_Res_Message2();
        Flight_Query_Ack_Message2 flight_query_ack_message2;
        flight_query_res_message2.setFlightArea(FlightArea);
        try {
            MessageServer.getInstance().getOos().writeObject(flight_query_res_message2);
            System.out.println("客户端发送请求成功！"+flight_query_res_message2);
            flight_query_ack_message2 = (Flight_Query_Ack_Message2) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+flight_query_ack_message2);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return flight_query_ack_message2.getFlightList();
    }




    /*查询员工信息*/
    public List<Admin> getAdminInforList() {
        Admin_Infor_Res_Message admin_infor_res_message=new Admin_Infor_Res_Message();
        Admin_Infor_Ack_Message admin_infor_ack_message;
        try{
            MessageServer.getInstance().getOos().writeObject(admin_infor_res_message);
            System.out.println("客户端发送请求成功！"+admin_infor_res_message);
            admin_infor_ack_message = (Admin_Infor_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+admin_infor_ack_message);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return admin_infor_ack_message.getAdminList();
    }

    /*查询退票订单*/
    public List<UserOrder> getRefundOrderList() {
        UserOrder_Refund_Res_Message userOrder_refund_res_message=new UserOrder_Refund_Res_Message();
        UserOrder_Refund_Ack_Message userOrder_refund_ack_message;
        try{
            MessageServer.getInstance().getOos().writeObject(userOrder_refund_res_message);
            System.out.println("客户端发送请求成功！"+userOrder_refund_res_message);
            userOrder_refund_ack_message = (UserOrder_Refund_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+userOrder_refund_ack_message);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return userOrder_refund_ack_message.getUserOrdersList();
    }


    /*查询已取消或已退款订单*/
    public List<UserOrder> getUserRefOrderList(String orderNo) {
        Query_Ref_Order_Ticket_Res_Message query_ref_order_ticket_res_message = new Query_Ref_Order_Ticket_Res_Message();
        Query_Ref_Order_Ticket_Ack_Message query_ref_order_ticket_ack_message;
        query_ref_order_ticket_res_message.setOrderNo(orderNo);
        try{
            MessageServer.getInstance().getOos().writeObject(query_ref_order_ticket_res_message);
            System.out.println("客户端发送请求成功！" + query_ref_order_ticket_res_message);
            query_ref_order_ticket_ack_message = (Query_Ref_Order_Ticket_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！" + query_ref_order_ticket_ack_message);
        }catch (IOException | ClassNotFoundException e){
            throw new RuntimeException(e);
        }
        return query_ref_order_ticket_ack_message.getUserOrderList();

    }



    /*查询订单信息*/
    public List<UserOrder> getUserOrderList() {
        UserOrder_Query_Res_Message userOrder_query_res_message=new UserOrder_Query_Res_Message();
        UserOrder_Query_Ack_Message userOrder_query_ack_message;
        try{
            MessageServer.getInstance().getOos().writeObject(userOrder_query_res_message);
            System.out.println("客户端发送请求成功！"+userOrder_query_res_message);
            userOrder_query_ack_message = (UserOrder_Query_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+userOrder_query_ack_message);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return userOrder_query_ack_message.getUserOrdersList();
    }



    /*查询旅客信息*/
    public List<Passenger> getPassengerInforList() {
        Passenger_Infor_Res_Message passenger_infor_res_message=new Passenger_Infor_Res_Message();
        Passenger_Infor_Ack_Message passenger_infor_ack_message;
        try{
            MessageServer.getInstance().getOos().writeObject(passenger_infor_res_message);
            System.out.println("客户端发送请求成功！"+passenger_infor_res_message);
            passenger_infor_ack_message = (Passenger_Infor_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+passenger_infor_ack_message);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return passenger_infor_ack_message.getPassengerList();
    }


    public List<Ticket> getTicketList() {
        Ticket_Query_Res_Message ticket_query_res_message = new Ticket_Query_Res_Message();
        Ticket_Query_Ack_Message ticket_query_ack_message;
        try {
            MessageServer.getInstance().getOos().writeObject(ticket_query_res_message);
            System.out.println("客户端发送请求成功！"+ticket_query_res_message);
            ticket_query_ack_message = (Ticket_Query_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！"+ticket_query_ack_message);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return ticket_query_ack_message.getTicketList();
    }

    /*按票务等级查询*/
    public List<Ticket> getTicketList1(String ticketType) {
        Query_Ticket_Res_Message query_ticket_res_message = new Query_Ticket_Res_Message();
        Query_Ticket_Ack_Message query_ticket_ack_message;
        query_ticket_res_message.setFclass(ticketType);
        try{
            MessageServer.getInstance().getOos().writeObject(query_ticket_res_message);
            System.out.println("客户端发送请求成功！" + query_ticket_res_message);
            query_ticket_ack_message = (Query_Ticket_Ack_Message) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！" + query_ticket_ack_message);
        }catch (IOException | ClassNotFoundException e){
            throw new RuntimeException(e);
        }
        return query_ticket_ack_message.getTicketList();
    }


    /*按订单号查询订单*/
    public List<UserOrder> getUserOrderList1(String order_id) {
        UserOrder_Query_Res_Message1 userOrder_query_res_message1 = new UserOrder_Query_Res_Message1();
        UserOrder_Query_Ack_Message1 userOrder_query_ack_message1;
        userOrder_query_res_message1.setUserOrder(order_id);
        try {
            MessageServer.getInstance().getOos().writeObject(userOrder_query_res_message1);
            System.out.println("客户端发送请求成功！" + userOrder_query_res_message1);
            userOrder_query_ack_message1 = (UserOrder_Query_Ack_Message1) MessageServer.getInstance().
                    getOis().readObject();
            System.out.println("客户端收到服务端的响应成功！" + userOrder_query_ack_message1);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return userOrder_query_ack_message1.getUserOrder();
    }
}
